/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ecommerce.taglib;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/**
 *
 * @author Yoonan
 */
public class uploadHandler extends TagSupport {

    private String name;
    private String path;
    private String clazz;
    private int limit_size;
    private String accept_type;

    @Override
    public int doStartTag() throws JspException {

        //Get the writer object for output.
        JspWriter out = pageContext.getOut();
        try {
            StringBuilder scriptCheckType = new StringBuilder();
            scriptCheckType.append("<script type=\"text/javascript\" >\n");
            scriptCheckType.append("    function checkExtension(fullPath) {\n");
            scriptCheckType.append("        var acceptType = \"%s\"\n");
            scriptCheckType.append("        if (fullPath) {\n");
            scriptCheckType.append("            var lastIndex = fullPath.lastIndexOf(\".\");\n");
            scriptCheckType.append("            var extension = fullPath.substring(lastIndex + 1);\n");
            scriptCheckType.append("            if (acceptType.indexOf(extension) > -1) {\n");
            scriptCheckType.append("                return true;\n");
            scriptCheckType.append("            }\n");
            scriptCheckType.append("            else {\n");
            scriptCheckType.append("                return false;\n");
            scriptCheckType.append("            }\n");
            scriptCheckType.append("        }\n");
            scriptCheckType.append("    }\n");
            scriptCheckType.append("</script>");
            
            StringBuilder pageContent = new StringBuilder();
            pageContent.append(String.format(scriptCheckType.toString(), accept_type));
            pageContent.append("<input type=\"file\" name=\"%s\" class=\"%s\" onchange=\"checkExtension(this.value);\" />");
            pageContent.append("<input type=\"hidden\" name=\"file_upload_path\" value=\"%s\" />");
            pageContent.append("<input type=\"hidden\" name=\"file_upload_limit\" value=\"%d\" />");
            pageContent.append("<input type=\"hidden\" name=\"file_upload_accept\" value=\"%s\" />");
            out.write(String.format(pageContent.toString(), name, clazz, path, limit_size, accept_type));
        } catch (IOException ex) {
            Logger.getLogger(uploadHandler.class.getName()).log(Level.SEVERE, null, ex);
        }

        return SKIP_BODY;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public int getLimit_size() {
        return limit_size;
    }

    public void setLimit_size(int limit_size) {
        this.limit_size = limit_size;
    }

    public String getAccept_type() {
        return accept_type;
    }

    public void setAccept_type(String accept_type) {
        this.accept_type = accept_type;
    }

}
